// pages/shopcart/shopcart.js
const personal = require('../../api/personal.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cart: [],
    totalMoney: 0,
    totalNum: 0,
    all: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  // 获取用户信息
  getUserInfo(token) {
    personal.getUserInfo(token).then(res => {
      if (res.data.code == 0) {
        this.setData({
          userInfo: res.data.data
        })
        this.getCarts()
        wx.hideLoading()
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    app.setNum()
    this.setData({
      all: false
    })
    let token = wx.getStorageSync('token')
    this.getUserInfo(token)
  },
  // 获取购物车缓存
  getCarts() {
    let arr = wx.getStorageSync('cart' + this.data.userInfo.id) || []
    let i = 0
    if (arr.length > 0) {
      this.setData({
        cart: arr
      })
      arr.forEach(value => {
        if (value.isSelect) {
          i++
        }
      })
      if (i == arr.length) {
        this.setData({
          all: true
        })
      }
    }else {
      this.setData({
        cart: []
      })
    }
    this.total()
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  toShop() {
    wx.switchTab({
      url: '../index/index',
    })
  },
  onChangeNum(e) {
    console.log(e)
    let num = e.detail
    let cart = wx.getStorageSync('cart' + this.data.userInfo.id) || []
    cart.forEach(value => {
      if (value.id == e.target.dataset.productid) {
        value.count = num
        if(value.inventory == num) {
          wx.showToast({
            title: '最多可购买' + value.inventory + '件',
            icon: 'none'
          })
        }
      }
    })
    wx.setStorageSync('cart' + this.data.userInfo.id, cart)
    app.setNum()
    this.total()
  },
  // 单个删除
  toDelete(e) {
    let that = this
    let cart = wx.getStorageSync('cart' + this.data.userInfo.id) || []
    wx.showModal({
      title: '确定删除吗？',
      success(res) {
        if (res.confirm) {
          cart.forEach((value, index) => {
            if (value.id == e.currentTarget.dataset.productid) {
              cart.splice(index, 1)
            }
          })
          wx.setStorageSync('cart' + that.data.userInfo.id, cart)
          that.setData({
            cart: cart,
            all: false,
          })
          app.setNum()
          that.total()
        } else if (res.cancel) {
          console.log('取消')
        }
      }
    })
  },
  // 清空购物车
  deleteAll() {
    let that = this
    let cart = wx.getStorageSync('cart' + this.data.userInfo.id) || []
    wx.showModal({
      title: '确定全部删除吗？',
      success(res) {
        if (res.confirm) {
          wx.removeStorageSync('cart' + that.data.userInfo.id)
          let cart = wx.getStorageSync('cart' + that.data.userInfo.id) || []
          that.setData({
            all: false,
            cart: cart
          })
          app.setNum()
        } else if (res.cancel) {
          console.log('取消')
        }
      }
    })
  },
  // 单选
  toSelect(e) {
    let cart = wx.getStorageSync('cart' + this.data.userInfo.id) || []
    let i = 0
    cart.forEach(value => {
      if (value.id == e.currentTarget.dataset.productid) {
        value.isSelect = !value.isSelect
      }
      if(value.isSelect) {
        i++
      }
    })
    if(i == cart.length) {
      this.setData({
        all: true
      })
    }else {
      this.setData({
        all: false
      })
    }
    this.setData({
      cart: cart
    })
    wx.setStorageSync('cart' + this.data.userInfo.id, cart)
    this.total()
  },
  // 全选
  selectAll() {
    this.setData({
      all: !this.data.all
    })
    let cart = this.data.cart
    if (this.data.all) {
      cart.forEach(value => {
        value.isSelect = true
      })
    } else {
      cart.forEach(value => {
        value.isSelect = false
      })
    }
    this.setData({
      cart: cart
    })
    wx.setStorageSync('cart' + this.data.userInfo.id, cart)
    this.total()
  },
  // 总价,总数量
  total() {
    let cart = wx.getStorageSync('cart' + this.data.userInfo.id)
    let sum = 0
    let num = 0
    for (let i = 0; i < cart.length; i++) {
      if (cart[i].isSelect) {
        sum += cart[i].count * cart[i].price
        num += cart[i].count
      }
    }
    this.setData({
      totalMoney: sum * 100,
      totalNum: num
    })
  },
  // 去结算
  onSubmit() {
    let cart = wx.getStorageSync('cart' + this.data.userInfo.id)
    let cart2 = cart.filter(value => {
      return value.isSelect == true
    })
    console.log(cart2)
    if(cart2.length ==0) {
      wx.showToast({
        title: '请选择商品',
        icon: 'none'
      })
    }else {
      wx.navigateTo({
        url: '../order/order',
      })
    }
  }
})